﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using SQLServerDataAccess;
using Tools;

namespace Frame.Models
{
    public class MenuFrist
    {
        public int ID;
        public string Icon;
        public string Name;
        public List<MenuSecond> MenuSeconds;
    }

    public class MenuSecond
    {
        public int ID;
        public string Name;
        public List<MenuThird> MenuThirds;
    }

    public class MenuThird
    {
        public int ID;
        public string Name;
        public string URL;
    }

    public class Main
    {
        public string UserID;
        public List<MenuFrist> MenuFrists;
    }

    public class MainModel
    {
        private Main _Main;
        private string _UserID;
        private DataTable _Menu;
        private DataAccess _DataAccess;

        public MainModel(string UserID)
        {
            _UserID = UserID;
            _DataAccess = new DataAccess();
        }

        public Main GetMain()
        {
            if (_UserID == "Administrator")
                _Menu = _DataAccess.GetTable(new CommandFill() { CommandText = "Select *, '' As HavePermissions From SY_FrameMenu Order By ListOrder" });
            else
                _Menu = _DataAccess.GetTable(new CommandFill() { CommandText = "SELECT SY_FrameMenu.*, MenuUser.UserID, MenuUser.Permissions AS HavePermissions FROM SY_FrameMenu LEFT OUTER JOIN (SELECT UserID, MenuID, Permissions FROM SY_FrameMenuUser WHERE UserID = @UserID) AS MenuUser ON SY_FrameMenu.AutoID = MenuUser.MenuID ORDER BY SY_FrameMenu.ListOrder", Parameters = new Dictionary<string, object>() { { "@UserID", _UserID } } });
            
            _Main = new Main() { UserID = _UserID, MenuFrists = new List<MenuFrist>() };
            FillData(0, null);
            return _Main;
        }

        private bool FillData(int Depth, object ParengObj)
        {
            DataRow[] DRs;
            MenuFrist MF = null;
            MenuSecond MS = null;
            MenuThird MT = null;
            bool R = false;

            switch (Depth)
            {
                case 0:
                    DRs = _Menu.Select("ParentID is Null", "ListOrder");
                    break;
                case 1:
                    MF = (MenuFrist)ParengObj;
                    DRs = _Menu.Select("ParentID = " + MF.ID.ToString(), "ListOrder");
                    break;
                case 2:
                    MS = (MenuSecond)ParengObj;
                    DRs = _Menu.Select("ParentID = " + MS.ID.ToString(), "ListOrder");
                    break;
                default:
                    return false;
            }

            foreach (DataRow DR in DRs)
            {
                switch (Depth)
                {
                    case 0:
                        MF = new MenuFrist() { ID = (int)DR["AutoID"], Name = DR["Name"].ToString(), Icon = DR["Icon"].ToString(), MenuSeconds = new List<MenuSecond>() };
                        if (FillData(1, MF))
                        {
                            _Main.MenuFrists.Add(MF);
                            R = true;
                        }
                        break;
                    case 1:
                        MS = new MenuSecond() { ID = (int)DR["AutoID"], Name = DR["Name"].ToString(), MenuThirds = new List<MenuThird>() };
                        if (FillData(2, MS))
                        {
                            MF.MenuSeconds.Add(MS);
                            R = true;
                        }
                        break;
                    case 2:
                        if (_UserID != "Administrator")
                            if (String.IsNullOrEmpty(DR["UserID"].ToString()))
                                continue;
                            else
                                MT = new MenuThird() { ID = (int)DR["AutoID"], Name = DR["Name"].ToString(), URL = AsynchronousLogin.GetAsynchronousLoginUrl(PreUrl.AddHttp(PreUrl.AddPermissions(DR["URL"].ToString(), DR["HavePermissions"].ToString())), DR["HavePermissions"].ToString()) };
                        else
                            MT = new MenuThird() { ID = (int)DR["AutoID"], Name = DR["Name"].ToString(), URL = AsynchronousLogin.GetAsynchronousLoginUrl(PreUrl.AddHttp(PreUrl.AddPermissions(DR["URL"].ToString(), DR["Permissions"].ToString())), DR["Permissions"].ToString()) };
                        MS.MenuThirds.Add(MT);
                        R = true;
                        break;
                }
            }

            return R;
        }
    }
}